/****************************************************************************
 * 
 *
 *  Copyright (C) 1995,1996,1997 RealNetworks, Inc.
 *  All rights reserved.
 *
 *  http://www.real.com/devzone
 *
 *  This program contains proprietary information of RealNetworks, Inc.,
 *  and is licensed subject to restrictions on use and distribution.
 *
 *  General time utility functions
 *
 */

#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "ppvtime.h"


int ppvTimeToString(const time_t* tTime, char* szTime, int nLen)
// converts a time data type to a string in the following format
// MM/DD/YYYY:HH:MM:SS
{
   struct tm* 	tmTime;
   int 		nResult;
   //long		lTimeZone;	

   // check for bad length
   if ( PPV_TIMESTRING_LEN > nLen )
   {
	// clear the string, if length will allow
	if (nLen > 1)
	{
	   strcpy(szTime, "");
	}
	return 0;
   }
 
   // convert to time stucture
   tmTime = localtime(tTime);

   // print the string
   nResult = strftime(szTime, PPV_TIMESTRING_LEN,
	    	      "%m/%d/%Y:%H:%M:%S", tmTime);
   
   if ( !nResult )
   {
	// clear the string, if length will allow
	if (nLen > 1)
 	{
	   strcpy(szTime, "");
	}
	return 0;
   }

   return 1;

}

// faster null-adding replacements for dangerous strncpy
void strn2cpy(char* t, const char* s) 
{                   
    t[0]=s[0];
    t[1]=s[1];
    t[2]='\0';
}

void strn4cpy(char* t, const char* s) 
{                   
    t[0]=s[0];
    t[1]=s[1];
    t[2]=s[2];
    t[3]=s[3];
    t[4]='\0';
}

int ppvStringToTime(const char* szTime, time_t* tTime)
// converts a string to the time data type.  the string must 
// be in the following format:
// MM/DD/YYYY:HH:MM:SS 
// MM/DD/YYYY HH:MM:SS  (undocumented but accepted)
{
   struct tm	tmTime;
   struct tm*	tmGetDST;
   time_t	lTime;
   int 		nResult;
   char		szBuffer[5] = "";

   time(&lTime);
   tmGetDST = localtime(&lTime);
 
   // check to make sure it is a good time string
   if ( PPV_TIMESTRING_LEN > strlen(szTime)+1 )
   {
	*tTime = 0;
	return 0;
   } else if ( ('/' != szTime[2]) ||
	       ('/' != szTime[5]) ||
	       (':' != szTime[10] && ' ' != szTime[10]) ||
	       (':' != szTime[13]) ||
	       (':' != szTime[16]) )
   {
	*tTime = 0;
	return 0;
   }

   // get the various values along the way, if they 
   // are bad just bail out....

   // get month
   strn2cpy(szBuffer, szTime);
   nResult = atoi(szBuffer);
   if ( (1 > nResult) ||
 	(12 < nResult) )
   {
	*tTime = 0;
	return 0;
   } else {
	tmTime.tm_mon = nResult-1;
   }

   // get day
   strn2cpy(szBuffer, szTime+3);
   nResult = atoi(szBuffer);
   if ( (1 > nResult) ||
	(31 < nResult) )
   {
	*tTime = 0;
	return 0;
   } else {
	tmTime.tm_mday = nResult;
   }

   // get year
   strn4cpy(szBuffer, szTime+6);
   nResult = atoi(szBuffer);
   if ( 1900 > nResult )
   {
	*tTime = 0;
	return 0;
   } else {
	tmTime.tm_year = (nResult-1900);
   }

   // get hour
   strn2cpy(szBuffer, szTime+11);
   nResult = atoi(szBuffer);
   if ( (0 > nResult) ||
	(23 < nResult) )
   {
	*tTime = 0;
	return 0;
   } else {
	tmTime.tm_hour = nResult;
   }

   // get minute
   strn2cpy(szBuffer, szTime+14);
   nResult = atoi(szBuffer);
   if ( (0 > nResult) ||
	(59 < nResult) )
   {
	*tTime = 0;
	return 0;
   } else {
	tmTime.tm_min = nResult;
   }

   // get second
   strn2cpy(szBuffer, szTime+17);
   nResult = atoi(szBuffer);
   if ( (0 > nResult) ||
    	(61 < nResult) ) // astronomers adding leap seconds??
   {
	*tTime = 0;
	return 0;
   } else { 
	tmTime.tm_sec = nResult;
   }

   // get daylight savings time
   tmTime.tm_isdst = -1;	 //tmGetDST->tm_isdst;

   *tTime = mktime(&tmTime);
   return 1;

}




